package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopCartServicelmpl implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加当前用户人购物车
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void addShopCart(ShoppingCartDTO shoppingCartDTO) {
        //把参数封装进购物车实体类中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //到当前用户人购物车里查询
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shopCartMapper.getBy(shoppingCart);
        if (cart != null) {
            //查到了就表示购物车里有,则数量加一
            cart.setNumber(cart.getNumber()+1);
            shopCartMapper.update(cart);
        } else {
            //如果没有找到就根据dishId/setmealId查询商品信息,封装数据,执行insert操作.
            Long dishId = shoppingCartDTO.getDishId();
            cart = new ShoppingCart();
            //添加用户id
            cart.setUserId(BaseContext.getCurrentId());
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            // 判断是不是菜品id
            if (dishId != null) {
                //根据id获取菜品信息
                Dish byId = dishMapper.getById(dishId);
                //传递菜品信息到购物车
                cart.setName(byId.getName());
                cart.setImage(byId.getImage());
                cart.setAmount(byId.getPrice());
                cart.setDishId(dishId);
                //获取菜品口味
                cart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            } else {
                //根据id获取套餐信息
                Setmeal setmeal = new Setmeal();
                List<Setmeal> setmealList = setmealMapper.list(setmeal);
                Setmeal setmeal1 = setmealList.get(0);

                cart.setName(setmeal1.getName());
                cart.setImage(setmeal1.getImage());
                cart.setAmount(setmeal1.getPrice());
                cart.setSetmealId(setmeal1.getId());
            }
            shopCartMapper.insert(cart);
        }
    }

    /**
     * 查询当前用户人购物车
     */
    @Override
    public List<ShoppingCart> list() {
        return shopCartMapper.getUserId(BaseContext.getCurrentId());
    }

    /**
     * 清空当前用户人购物车
     */
    @Override
    public void clean() {
        shopCartMapper.deletUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中一个商品
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //把参数封装进购物车实体类中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查询当前用户购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shopCartMapper.getBy(shoppingCart);
        //判断查询结果值是否大于1
        if (cart.getNumber() != 1) {
            //数量减一
            cart.setNumber(cart.getNumber() - 1);
            shopCartMapper.update(cart);
        } else {
            shopCartMapper.sub(cart);
        }
    }
}
